<template>
  <div>
    <!-- 卡片区域 -->
    <el-card shadow="hover">
      <el-form>
        <el-row :gutter="5">
          <!-- 订单状态 -->
          <el-col :span="6">
            <el-select
              v-model="queryInfo.status"
              placeholder="请选择状态"
              @change="filterByStatus"
              @clear="getOrdersList"
              clearable
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-col>
          <!-- 订单查询 -->
          <el-col :span="8">
            <el-input
              placeholder="请输入订单ID"
              v-model="queryInfo.order_id"
              @change="filterByOrder"
              clearable
            ></el-input>
          </el-col>
          <!-- 查询按钮 -->
          <el-col :span="4">
            <el-button type="primary">查询</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <!-- 表格区域 -->
    <el-table
      :data="res"
      fit
      border
      :header-cell-style="{ background: '#ffd0bd', color: '#606266' }"
      v-loading="loading"
    >
      <el-table-column label="#" type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="ID">
              <span>{{ props.row.id }}</span>
            </el-form-item>
            <el-form-item label="订单编号">
              <span>{{ props.row.order_number }}</span>
            </el-form-item>
            <el-form-item label="状态">
              <span>{{ props.row.status_desc }}</span>
            </el-form-item>
            <el-form-item label="商品名称">
              <div v-for="item in props.row.foods" :key="item.name">
                <span>{{
                  item.name + ' ' + item.price + ' × ' + item.quantity
                }}</span>
              </div>
            </el-form-item>
            <el-form-item label="备注">
              <span>{{ props.row.note }}</span>
            </el-form-item>
            <el-form-item label="金额">
              <span>{{ props.row.price }}</span>
            </el-form-item>
            <el-form-item label="姓名">
              <span>{{ props.row.member.nickname }}</span>
            </el-form-item>
            <el-form-item label="电话">
              <span>{{ props.row.member.mobile }}</span>
            </el-form-item>
            <el-form-item label="地址">
              <span>{{ props.row.member.address }}</span>
            </el-form-item>
            <el-form-item label="创建时间">
              <span>{{ props.row.created_time }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <!-- 表格 -->
      <el-table-column
        label="订单ID"
        align="center"
        prop="id"
      ></el-table-column>
      <el-table-column label="订单状态" align="center">
        <template slot-scope="scope">
          <el-tag
            size="medium"
            :type="tagType(scope.row.status_desc)"
            effect="dark"
          >
            {{ scope.row.status_desc }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="金额"
        align="center"
        prop="price"
      ></el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="created_time"
      ></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="danger"
            :disabled="scope.row.status_desc != '待发货'"
            :plain="scope.row.status_desc == '待发货'"
            @click="handleSend(scope.row)"
          >
            {{ sended(scope.row.status_desc) }}
          </el-button>
          <!-- 查看 -->
          <el-button size="small" @click="handleView(scope.row)">
            详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 页码 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </div>
</template>

<style>
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}

.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>

<script>
export default {
  data() {
    return {
      // 获取订单列表
      queryInfo: {
        // 根据状态筛选
        status: '',
        // 根据订单编号查询
        order_id: '',
        // 当前的页数
        pagenum: 1,
        // 当前每页显示多少数据
        pagesize: 10,
      },
      options: [
        {
          value: '1',
          label: '待评价',
        },
        {
          value: '2',
          label: '已完成',
        },
        {
          value: '-6',
          label: '待确认',
        },
        {
          value: '-7',
          label: '待发货',
        },
        {
          value: '-8',
          label: '已关闭',
        },
        {
          value: '-16',
          label: '待支付',
        },
      ],
      loading: true,
      total: 0,
      res: [],
    }
  },
  computed: {},
  watch: {
    $route() {
      //对路由变化作出响应
      this.getOrdersList()
    },
  },
  methods: {
    tableRowClassName({ rowIndex }) {
      if (rowIndex === 1) {
        return 'warning-row'
      } else if (rowIndex === 3) {
        return 'success-row'
      }
      return ''
    },
    // // 公共方法
    getOrdersList() {
      this.getOrdersLists(this.queryInfo)
    },
    async getOrdersLists(query) {
      this.loading = true
      const path = this.URL.Orders
      let temp = []
      const { data: result } = await this.$http
        .get(path, {
          params: query,
        })
        .catch(() => {
          this.$message.error('NETWORK ERROR')
        })

      result['list'].forEach((element) => {
        temp.push(element)
      })
      this.res = temp
      this.total = result.total
      if (result.flag) {
        this.$message({
          type: 'info',
          message: '消息通知: 您有新的订单!',
          duration: 10000,
        })
      }
      this.loading = false
    },
    async getUserList() {},
    // 确定发货
    async handleSend(row) {
      const confirmResult = await this.$confirm('确定要发货吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })

      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消')
      }
      // 已发货请求
      const path = this.URL.SendOrders
      const { data: res } = await this.$http
        .patch(path, {
          data: {
            id: row.id,
          },
        })
        .catch(() => {
          this.$message.error('NETWORK ERROR')
        })
      if (res.code !== 200) {
        this.$message.error('操作失败，请重试！')
      } else {
        //更新状态
        row.status_desc = res.status_desc
        this.$message.success('操作成功')
      }
    },
    //已经发货
    sended: function (status_desc) {
      if (status_desc == '待发货') {
        return '确定发货'
      } else {
        return '已经发货'
      }
    },
    // 查看详情
    handleView: function (row) {
      console.log(row)
      this.$router.push({ name: 'ViewOrders', params: row })
    },
    // 根据状态筛选
    filterByStatus: function () {
      this.getOrdersLists(this.queryInfo)
    },
    // 根据订单编号查询
    filterByOrder: function () {
      this.getOrdersLists(this.queryInfo)
    },
    //标签type
    tagType: function (status_desc) {
      var type = ''
      switch (status_desc) {
        case '待发货':
          type = 'danger'
          break
        case '待确认':
          type = 'warning'
          break
        case '待评价':
          type = ''
          break
        case '已完成':
          type = 'success'
          break
        default:
          type = 'info'
          break
      }
      return type
    },

    // 监听 pagesize 改变的事件
    handleSizeChange(pagesize) {
      this.queryInfo.pagesize = pagesize
      // 重新请求
      this.getOrdersList()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(pagesize) {
      this.queryInfo.pagenum = pagesize
      // 重新请求
      this.getOrdersList()
    },
  },
  created() {
    this.getOrdersList()
    window.setInterval(() => {
      setTimeout(this.getOrdersList(), 60000)
    }, 60000)
  },
}
</script>

<style lang="less" scoped>
.state {
  text-align: center;
}
</style>